package com.jsgt.greekfire.ui.wallet.assets

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.jsgt.greekfire.App
import com.jsgt.greekfire.ENTRUST_NUM
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AtrAccountInfoBean
import com.jsgt.greekfire.bean.TradeOrder
import com.jsgt.greekfire.common.util.UtilsBigDecimal
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.viewmodel.UserViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.activity_assets.*

class AssetsActivity : BaseActivity(), View.OnClickListener {
    private val userViewModel1: UserViewModel by lazy {
        ViewModelProvider(App.context).get(UserViewModel::class.java)
    }
    private val userViewModel: UserViewModel by lazy {
        ViewModelProvider(this).get(UserViewModel::class.java)
    }
    private val orderAdapter: TradeOrderAdapter by lazy {
        TradeOrderAdapter()
    }
    private val orderList: MutableList<TradeOrder> by lazy {
        ArrayList<TradeOrder>()
    }

    private var wallet: IdentityWallet? = null
    private var asset: AtrAccountInfoBean? = null
    private var balance = 0.0
    private var page = 1
    private val limit = 20

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_assets
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        wallet = intent.getParcelableExtra("wallet")
        if (wallet == null) {
            finish()
        }
        title = wallet!!.key

        txvReceive.setOnClickListener(this)
        txvTransfer.setOnClickListener(this)
        txvCoin.text = wallet!!.key
        txvFrozen.visibility = View.GONE
        refreshLayout.setRefreshHeader(MaterialHeader(this))
        refreshLayout.setOnRefreshListener {
            page = 1
            wallet?.let {
                userViewModel.getTradeOrderList(
                    it.address!!,
                    null,
                    page.toString(),
                    limit.toString()
                )
                userViewModel1.getAccount(it.address!!, "true")
            }
        }
        refreshLayout.setOnLoadMoreListener {
            page++
            wallet?.let {
                userViewModel.getTradeOrderList(
                    it.address!!,
                    null,
                    page.toString(),
                    limit.toString()
                )
            }
        }
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = orderAdapter
        orderAdapter.setNewInstance(orderList)
        orderAdapter.address = wallet!!.address

        userViewModel1.getAccountLiveData.observe(this, Observer {
            asset = it
            refreshAssetView()
        })

        userViewModel.tradeOrderPageLiveData.observe(this, Observer {
            if (page == 1) {
                orderList.clear()
                refreshLayout.finishRefresh()
            }
            orderList.addAll(it.data)
            orderAdapter.notifyDataSetChanged()
            if (it.data.size < limit) {
                refreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                refreshLayout.finishLoadMore()
            }
        })
        userViewModel.errorLiveData.observe(this, Observer {
            refreshLayout.finishLoadMore()
        })
        refreshLayout.autoRefresh()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txvTransfer -> {
//                ToastUtils.show(R.string.not_yet_open)
                goToActivity<SendActivity> {
                    this.putExtra("wallet", wallet)
                }
            }
            R.id.txvReceive -> {
                goToActivity<ReceiveActivity> {
                    this.putExtra("wallet", wallet)
                }
            }
        }
    }

    private fun refreshAssetView() {
        asset?.let {
            if (!TextUtils.isEmpty(it.balance)) {
                balance = UtilsBigDecimal.div(it.balance!!, ENTRUST_NUM)
                txvAmount.text = balance.toString()
            } else {
                balance = 0.0
                txvAmount.text = balance.toString()
            }
            it.delegate_frozen?.let { it1 ->
                txvFrozen.visibility = View.VISIBLE
                txvFrozen.text = String.format(
                    getString(R.string.mining_frozen),
                    UtilsBigDecimal.div(it1, ENTRUST_NUM).toString(),
                    wallet!!.key
                )
            }
        }
    }

    var isFirst = true
    override fun onResume() {
        super.onResume()
        if (!isFirst){
            refreshLayout.postDelayed({
                refreshLayout.autoRefresh()
            },500)
        }else{
            isFirst = false
        }
    }
}